<%@ page import="org.spal.Producto"%>
<!DOCTYPE html>
<html>
<head>
<meta name="layout" content="main">
<meta name="keywords" content="jquery,ui,easy,easyui,web">
<meta name="description"
	content="easyui help you build your web page easily!">
<title>jQuery EasyUI Demo</title>
<link rel="stylesheet" type="text/css"
	href="http://www.jeasyui.com/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
	href="http://www.jeasyui.com/easyui/themes/icon.css">
<script type="text/javascript"
	src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
<script type="text/javascript"
	src="http://www.jeasyui.com/easyui/jquery.easyui.min.js"></script>
<style type="text/css">
.products{
			list-style:none;
			margin-right:30px;
			padding:0px;
			height:100%;
		}
		.products li{
			display:inline;
			float:left;
			margin:10px;
		}
		.item{
			display:block;
			text-decoration:none;
		}
		.item img{
			border:1px solid #333;
		}
		.item p{
			margin:0;
			font-weight:bold;
			text-align:center;
			color:#c3c3c3;
		}
		.cart{
			position:fixed;
			right:0;
			top:0;
			width:350px;
			height:100%;
			background:#ccc;
			padding:0px 5px;
		}
		h1{
			text-align:center;
			color:#555;
		}
		h2{
			position:absolute;
			font-size:16px;
			left:10px;
			bottom:20px;
			color:#555;
		}
		.total{
			margin:0;
			text-align:right;
			padding-right:20px;
		}
</style>
<script>
var data = {"total":0,"rows":[]};
	var totalCost = 0;


	$(function(){
		$('#cartcontent').datagrid({
			singleSelect:true
		});
		$('.item').draggable({
			revert:true,
			proxy:'clone',
			onStartDrag:function(){
				$(this).draggable('options').cursor = 'not-allowed';
				$(this).draggable('proxy').css('z-index',10);
			},
			onStopDrag:function(){
				$(this).draggable('options').cursor='move';
			}
		});
		$('.cart').droppable({
			onDragEnter:function(e,source){
				$(source).draggable('options').cursor='auto';
			},
			onDragLeave:function(e,source){
				$(source).draggable('options').cursor='not-allowed';
			},
			onDrop:function(e,source){
				var name = $(source).find('p:eq(0)').html();
				var price = $(source).find('p:eq(1)').html();
				addProduct(name, parseFloat(price.split('$')[1]));
			}
		});
	});

	function addProduct(name,price){
		function add(){
			for(var i=0; i<data.total; i++){
				var row = data.rows[i];
				if (row.name == name){
					row.quantity += 1;
					return;
				}
			}
			data.total += 1;
			data.rows.push({
				name:name,
				quantity:1,
				price:price,
                remove: '<button type="button" onclick="removeProduct(this, event)">x</button>',
                decrement: '<button type="button" onclick="removeProduct(this, event)">-</button>',
			});
		}
		add();
		totalCost += price;
		$('#cartcontent').datagrid('loadData', data);
		$('div.cart .total').html('Total: $'+totalCost);
	}
	
	function rmProdut(name, price) {
	    var tr = $(el).closest('tr');
	    var name = tr.find('td[field=name]').text();
	    var price = tr.find('td[field=price]').text();
	    var quantity = tr.find('td[field=quantity]').text();
	    for(var i = 0; i < data.total; i++){
	        var row = data.rows[i];
	        if (row.name == name) {
	            data.rows.splice(i, 1);
	            data.total--;
	            break;
	        }
	    }
	    totalCost -=  price * quantity;
	    $('#cartcontent').datagrid('loadData', data);
	    $('div.cart .total').html('Total: $'+totalCost);
	}

	function removeProduct(el, event) {
	    var tr = $(el).closest('tr');
	    var name = tr.find('td[field=name]').text();
	    var price = tr.find('td[field=price]').text();
	    var quantity = tr.find('td[field=quantity]').text();
	    for(var i = 0; i < data.total; i++){
	        var row = data.rows[i];
	        if (row.name == name) {
	            data.rows.splice(i, 1);
	            data.total--;
	            break;
	        }
	    }
	    totalCost -=  price * quantity;
	    $('#cartcontent').datagrid('loadData', data);
	    $('div.cart .total').html('Total: $'+totalCost);
	}
	</script>
</head>
<body style="margin: 0; padding: 0; height: 100%; background: #fafafa;">
	<ul class="products">
		<g:each var="producto" in="${productos}" status="i">
			<li><a href="#" class="item"> <img
					src="${createLink(action: 'displayImg', id:producto.id)}"
					width="162" height="162"></img>
					<div>
						<p>
							${producto.nombre}
						</p>
						<p>
							Precio:$
							${producto.precio}
						</p>
					</div>
			</a></li>
		</g:each>
	</ul>
	<div class="cart">
		<h1>Carro de compra</h1>
		<div style="background: #fff">
			<table id="cartcontent" fitColumns="true"
				style="width: 350px; height: auto;">
				<thead>
					<tr>
						<th field="name" width=140>Nombre</th>
						<th field="quantity" width=60 align="right">Cantidad</th>
						<th field="price" width=60 align="right">Precio</th>
						<th field="remove" width=60 align="right"></th>
						<th field="decrement" width=60 align="right"></th>
					</tr>
				</thead>
			</table>
		</div>
		<p class="total">Total: $0</p>
		<h2>Desliza aquí para agregar al carro</h2>
	</div>
</body>
</html>